import { _list, _floorlist, _companylist } from '../../common/visit'
const app = getApp()
Page({
  data: {
    result: null,
    floors: [],
    companies: [],
    companyIndex: null,
    floorIndex: null
  },
  totalQuery() {
    app.loading('加载中')
    _list()
      .then(res => {
        wx.hideLoading()
        const { IsSuccess, Data } = res.data
        if (IsSuccess) {
          this.data.result = Data[0].qrCellVOs || []
          this.data.floors = this.data.result.map(item => item.cellname)
          this.setData({
            floors: this.data.floors
          })
        }
      })
      .catch(err => {
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: JSON.stringify(err),
          showCancel: false
        })
      })
  },
  longcXz(e) {
    let floorIndex = e.currentTarget.dataset.index
    let companyIndex = null
    this.setData({
      floorIndex,
      companyIndex
    }, () => {
      const { result } = this.data
      this.setData({
        companies: result[floorIndex] ? result[floorIndex].qrHouses : []
      })
    })
  },
  gongsXz(e) {
    this.setData({
      companyIndex: e.target.dataset.index
    })
  },
  goNext() {
    if (this.data.floorIndex === null) {
      app.toast('请选择楼层')
      return
    }
    if (this.data.companyIndex === null) {
      app.toast('请选择公司')
      return
    }
    let id = this.data.companies[this.data.companyIndex].houseid
    let cname = this.data.companies[this.data.companyIndex].housename
    wx.navigateTo({
      url: `./visitor2?id=${id}&cname=${cname}`
    })
  },
  onLoad(options) {
  },
  onReady() { },
  onShow() {
    app.memberReadyCb = () => {
      this.totalQuery()
    }
    app.fansReadyCb = () => {
      app.checkMember()
    }
    app.init()
  },
  onShareAppMessage() {
    return app.shareInfo
  }
})